package com.xysq.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.umeng.analytics.MobclickAgent;
import com.xysq.adapter.ViewPagerAdapter;
import com.xysq.fragment.OrderListFragment;
import com.xysq.lemon.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.InjectView;

public class MyOrderActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    @InjectView(R.id.ibtn_back)
    ImageButton backIbtn;
    @InjectView(R.id.group_tabs)
    RadioGroup tabsGroup;
    @InjectView(R.id.rbtn_pending)
    RadioButton pendingRbtn;
    @InjectView(R.id.rbtn_finished)
    RadioButton finishedRbtn;
    @InjectView(R.id.rbtn_canceled)
    RadioButton canceledRbtn;
    @InjectView(R.id.view_pager)
    ViewPager viewPager;

    private OrderListFragment pendingFragment;
    private OrderListFragment finishedFragment;
    private OrderListFragment canceledFragment;
    private List<Fragment> fragmentList = new ArrayList<Fragment>();
    private ViewPagerAdapter pagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_order);

        tabsGroup.setOnCheckedChangeListener(this);
        pendingFragment = OrderListFragment.newInstance(OrderListFragment.TYPE_PENDING, OrderListFragment.PAGE_ORDER);
        finishedFragment = OrderListFragment.newInstance(OrderListFragment.TYPE_FINISHED, OrderListFragment.PAGE_ORDER);
        canceledFragment = OrderListFragment.newInstance(OrderListFragment.TYPE_CENCELED, OrderListFragment.PAGE_ORDER);
        fragmentList.add(pendingFragment);
        fragmentList.add(finishedFragment);
        fragmentList.add(canceledFragment);
        pagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), fragmentList);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setOffscreenPageLimit(fragmentList.size());
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                switch (i) {
                    case 0:
                        pendingRbtn.setChecked(true);
                        pendingFragment.reloadData();
                        break;
                    case 1:
                        finishedRbtn.setChecked(true);
                        finishedFragment.reloadData();
                        break;
                    case 2:
                        canceledRbtn.setChecked(true);
                        canceledFragment.reloadData();
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        viewPager.setCurrentItem(0);
        tabsGroup.check(R.id.rbtn_pending);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rbtn_pending:
                viewPager.setCurrentItem(0);
                break;
            case R.id.rbtn_finished:
                viewPager.setCurrentItem(1);
                break;
            case R.id.rbtn_canceled:
                viewPager.setCurrentItem(2);
                break;
        }
    }

    public void onBack(View view) {
        onBackPressed();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("我下的单页");
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("我下的单页");
        MobclickAgent.onPause(this);
    }
}
